package banco;

import java.util.List;

import modelo.Empresa;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class EmpresaDAO {
	
	public void saveOrUpdate(Empresa e) {
		Session sessao = null;
		Transaction tx = null;
		try {
			sessao = HibernateUtil.getSession();
			tx = sessao.beginTransaction();
			sessao.saveOrUpdate(e);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			ex.printStackTrace();
		} finally {
			if (sessao != null) {
				try {
					sessao.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
	}
	
	
	
	public List<Empresa> listar() {
		Session sessao = null;
		Transaction tx = null;
		try {
			sessao = HibernateUtil.getSession();
			tx = sessao.beginTransaction();
			Query q = sessao.getNamedQuery("Empresa.listar");
			@SuppressWarnings("unchecked")
			List<Empresa> lista = q.list();
			tx.commit();
			return lista;
		} catch (Exception ex) {
			tx.rollback();
			ex.printStackTrace();
			return null;
		} finally {
			if (sessao != null) {
				try {
					sessao.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	

}
